﻿using System;
using System.Collections.Generic;
using System.Text;
using GeneratorLibrary.Client.Base;

namespace MySqlGeneratorLibrary {
    public class MySqlCrudTable : CrudTableBase {

        public override CrudColumnBase NewColumn(System.Data.DataRow dr) {
            return new MySqlCrudColumn(dr);
        }
        protected override void SelectIdentityColumn(StringBuilder sb, string identityName) {
            sb.Append("\r\n\r\n\tselect ");
            sb.Append(GetParameterPrefix(true));
            sb.Append(identityName);
            sb.Append(" = SCOPE_IDENTITY()");
        }

        protected override void WriteGrant(string storedProcedure) {
            StringBuilder sb = new StringBuilder();
            if (this.grantAccess.Length > 0) {
                sb.Append("GRANT EXECUTE ON ");
                sb.Append(this.tableName);
                sb.Append(storedProcedure);
                sb.Append(" TO ");
                sb.Append(this.grantAccess);
            }
            this.currentGrantSql = sb.ToString();
        }
        protected override void WriteComments(StringBuilder sb, string description) {
            sb.Append("-- =============================================");
            sb.Append("\r\n-- Author:\t\t");
            sb.Append(author);
            sb.Append("\r\n-- Create date:\t");
            sb.Append(DateTime.Now.ToShortDateString());
            sb.Append("\r\n-- Description:\t");
            sb.Append(description);
            sb.Append("\r\n-- Revisions:\t");
            sb.Append("\r\n-- =============================================\r\n");
        }
        protected override bool DeclareColumn(StringBuilder sb, CrudColumnBase c, bool first, bool output, bool setToNull) {
            if (!first) {
                sb.Append(",\r\n");
            } else {
                first = !first;
            }

            sb.Append("\t");
            sb.Append(GetParameterPrefix(output));
            sb.Append(c.Name + " ");

            sb.Append(c.QualifiedDataType);

            if (output) {
                sb.Append(" OUTPUT");
            } else if (c.IsNullable || setToNull) {
                sb.Append(" = NULL");
            }

            return first;
        }
        protected override void EndMethodBody(StringBuilder sb, bool hasResultset) {
            sb.Append("\r\nEnd\r\n");
        }

        protected override void StartMethodBody(StringBuilder sb, bool hasResultset) {
            sb.Append("\r\nAS\r\nBegin\r\n\tSET NOCOUNT ON\r\n");
        }

        protected override void StartMethodBody(StringBuilder sb, bool hasResultset, string commentsDescription) {
            sb.Append("\r\nAS\r\nBegin\r\n\tSET NOCOUNT ON\r\n");
        }

        protected override void PrintTablePrefix(StringBuilder sb) {
            sb.Append("");
        }

        protected override void CreateProcedureDeclaration(StringBuilder sb, string procName) {
            sb.Append("Create Procedure ");
            sb.Append(procName);
            sb.Append("\r\n");
        }

        protected override ProcLocation GetWhereToPutComments() {
            return ProcLocation.BeforeProcedureDeclaration;
        }

        protected override void CloseCRUDStatement(StringBuilder sb) {
            //do nothing
        }

        protected override string GetParameterPrefix(bool isOutput) {
            return "@";
        }

    }
}
